// 读取角色和部门数据数据
function searchUser(usernamet, namet) {
	username = trim_value(usernamet);
	name = trim_value(namet);
	send(user);
}
var updateUserDialog = new Dialog( {
	showdiv :"updateUserDialog",
	title :"修改用户信息",
	x :400,
	y :300
});
rootDialog.childDialog = updateUserDialog;
var createUserDialog = new Dialog( {
	showdiv :"createUserDialog",
	title :"创建用户信息",
	x :400,
	y :300
});
rootDialog.childDialog = createUserDialog;

function initRoleAndDept(useruuid) {
	if (useruuid != null && useruuid != "" && useruuid != -9) {
		$Q('password').value = "********";
		$Q('rpassword').value = "********";
		$Q('password').disabled = true;
		$Q('rpassword').disabled = true;
		$Q('username').disabled = true;
	} else {
		$Q('password').value = "";
		$Q('rpassword').value = "";
		$Q('password').disabled = false;
		$Q('rpassword').disabled = false;
		$Q('username').disabled = false;
	}
	if (useruuid != null && useruuid != "") {
		User.chakan(useruuid, function(data) {
			for ( var i = 0; i < data.length; i++) {
				if (data[i].tname == 'state') {
					DWRUtil.setValue(data[i].tname, data[i].tval);
				} else {
					$(data[i].tname).value = data[i].tvalue;
				}
			}
		});
	}

}
function newUser() {
	var username = trim_value(DWRUtil.getValue('username'));
	var name = trim_value(DWRUtil.getValue('name'));
	var state = DWRUtil.getValue('state');
	var password = trim_value(DWRUtil.getValue('password'));
	var rpassword = trim_value(DWRUtil.getValue('rpassword'));
	var useruuid = DWRUtil.getValue('useruuid');
	var email = DWRUtil.getValue('email');
	if (useruuid != null && useruuid != "") {
		User.updateUser(useruuid, name, state,email, function(data) {
			if (data == 1) {
				send(user);
				alert('用户修改成功！');
			} else {
				alert('用户修改失败！');
			}
			closeDialog(updateUserDialog);
		});
	} else {
		var checkpwd = false;
		var useruuid = DWRUtil.getValue('useruuid');
		if (password != rpassword || password == '') {
			$Q("passworderror").innerHTML = "两次密码输入不一致或空";
			checkpwd = false;
		} else {
			$Q("passworderror").innerHTML = "正确！";
			checkpwd = true;
		}

		User.checkUsername(username, function(data) {
			if (data == 1) {
				$Q("usernameerror").innerHTML = "正确！";
				if (checkpwd) {
					User.addUser(username, name, password, state,email,
							function(data) {
								if (data == 1) {
									send(user);
									alert('添加用户成功！');
								} else {
									alert('添加用户失败！' + data);
								}
								closeDialog(createUserDialog);
							});
				}
			} else {
				$Q("usernameerror").innerHTML = "用户名已存在或包含非法字符！";
			}
		});

	}

}

var chakancellfuncs = [ function(data) {
	return data.title;
}, function(data) {
	return data.tvalue;
} ];

var cellfuncs = [
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				rootDialog.showdivWidth = 600;
				rootDialog.showdivHeight = 500;
				rootDialog.title = "查看用户信息";
				openDialog(rootDialog, '', function() {
					var chakantable = document.createElement("table");
					chakantable.style.align = "center";
					chakantable.className = "table";
					rootDialog.contentdiv.appendChild(chakantable);
					var chakantbody = document.createElement("tbody");
					chakantbody.setAttribute('id', 'chakantbody');
					chakantable.appendChild(chakantbody);
					User.chakan(data.useruuid, function(data) {
						DWRUtil.removeAllRows('chakantbody');
						DWRUtil.addRows('chakantbody', data, chakancellfuncs,
								tableclass);
					});
				});
			};
			href.innerHTML = data.username;
			return href;
		},
		function(data) {
			return data.name;
		},
		function(data) {
			return data.userlevel;
		},
		function(data) {
			return data.state == '1' ? '有效' : '无效';
		},
		function(data) {
			// return data.dept;
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				initDept(data.useruuid, "user");
			};
			href.innerHTML = '部门列表';
			return href;
		},
		function(data) {
			// return data.role;
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				initSelectRoles(data.useruuid, "user");
			};
			href.innerHTML = '角色列表';
			return href;

		},
		function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				openDialog(updateUserDialog,
						URLParams['popBasePath'] + '/users/add.htm', function() {
							initRoleAndDept(data.useruuid);
						});
			};
			href.innerHTML = '修改';
			return href;
		}, function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				if (confirm('确认初始化(' + data.username + ')密码吗?')) {
					User.initPassword(data.useruuid, function(data) {
						if (data == 1) {
							alert('密码初始化成功！');
						} else {
							alert('密码初始化失败！');
						}
					});
				}
				;
			};
			href.innerHTML = '初始化密码';
			return href;
		}, function(data) {
			if (data.useruuid == "1234567890") {
				return "";
			} else {
				var href = document.createElement('a');
				href.setAttribute('href', '#');
				href.onclick = function() {
					if (confirm('确认删除用户(' + data.username + ')吗?')) {
						User.removeUserById(data.useruuid, function(data) {
							if (data == 1) {
								send(user);
								alert("删除成功！");
							} else {
								alert("删除失败！");
							}
						});
					}
					;
				};
				href.innerHTML = '删除';
				return href;
			}
		} ];

var username = "";
var name = "";
var user = {
	navigationsize :10,
	paramname :'user',
	page :1,
	funcname :'User.findUserByLikeUserName(username,name,)',
	cellfuncs :cellfuncs,
	pagesize :10,
	count :'getCountUserByLikeUserName(username,name,)',
	idname :'userlistZone'
};

var pageuuid = user.paramname;
window.onload = function() {
	initDWR();
	useLoading('../../expimages/blue-loading.gif', 'img');
	User.checkPopAnd(8, function(data) {
		if (data) {
			send(user);
		} else {
			alert("请勿非法访问，仅拥有（权限管理）权限的用户可以访问！");
			top.window.location = URLParams['basePath'];
		}
	});
};
